Skip to content

fix(auth): mirror soroban-core #38 injective hash_payload encoding (A1)#41

Closed
AquiGorka wants to merge 1 commit into
devfrom
fix/a1-signing-encoding
Closed

fix(auth): mirror soroban-core #38 injective hash_payload encoding (A1)#41
AquiGorka wants to merge 1 commit into
devfrom
fix/a1-signing-encoding

Conversation

@AquiGorka

Copy link
Copy Markdown
Contributor

What

Update the off-chain P256 signing pre-image in buildAuthPayloadHash to be byte-identical to soroban-core's hash_payload (modules/primitives/src/lib.rs) as changed by soroban-core #38 (finding A1, head efb41c8).

New encoding, matching the contract exactly:

contractId (strkey string bytes) ++ ToXdr(Vec<Condition>) ++ live_until_ledger (4-byte little-endian)

SHA-256 is applied by the signer (crypto.subtle.sign, ECDSA/SHA-256), so this function returns the un-hashed pre-image.

  • Conditions are serialized via their canonical ScVal-vector XDR (ScVal.scvVec(conditions.map(c => c.toScVal())).toXDR()), in input order — never sorted or bucketed. This mirrors the contract's conditions.to_xdr(e).
  • The old per-type back-to-back bucket concatenation (Create/Deposit/Withdraw buckets, raw address bytes + 16-byte LE amounts) is removed. Because XDR length-prefixes vectors and variant-tags enums, the encoding is injective: ExtDeposit(X, a) and ExtWithdraw(X, a) over the same address/amount now hash differently, and reordering changes the digest.

Why

The on-chain verifier hashes this exact pre-image and checks the P256 signature against it. If the off-chain signer diverges by a single byte, every on-chain signature verification fails. This must land and deploy in lockstep with soroban-core #38.

Cross-check test

src/utils/auth/build-auth-payload.unit.test.ts asserts the SDK digest equals the contract's hash_payload output for fixed vectors (generated by running the contract's own hash_payload):

  • [ExtDeposit(G…, 1000)], live 100 → 4482986e…e75b73
  • [ExtWithdraw(G…, 1000)], live 100 → bb0354a9…3bc57e (and asserts deposit ≠ withdraw)
  • [ExtDeposit(G…, 42), Create([7;65], 99)], live 555 → 506acd6b…32faee

Fixed strkeys are shared with the contract KAT (StrKey.encodeContract(0x11×32) / encodeEd25519PublicKey(0x22×32)).

Verification

  • deno lint — pass
  • deno task test:unit — 14 passed, 0 failed (includes the new cross-check)
  • deno publish --dry-run --allow-slow-types — type-check clean
  • Pre-existing on dev, unrelated to this change: deno check reports stellar-plus import-map errors in test helpers / sorobanSmartContractId.ts / mod.ts, and deno fmt --check drifts on bundle-auth-entry.ts / deposit-auth-entry.ts / deno.json newline. CI (test.yml) runs deno lint + deno task test only (fmt is commented out).

deno.json patch bump 0.6.1 → 0.6.2.

Mirrors soroban-core #38 @ efb41c83d8b725e0423dbf5485abf40a54f2811d. If #38's encoding shifts before merge, this PR re-syncs (regenerate the KAT vectors).

Do not merge — lands in lockstep with soroban-core #38.

Replace the legacy per-type bucketed concatenation in buildAuthPayloadHash
with the byte-identical encoding used by soroban-core's hash_payload
(modules/primitives/src/lib.rs, PR #38, A1):

  contractId (strkey bytes) ++ ToXdr(Vec<Condition>) ++ live_until_ledger (4-byte LE)

hashed with SHA-256 by the P256 signer. Conditions are serialized via their
canonical ScVal-vector XDR in input order (never sorted/bucketed), so
ExtDeposit(X,a) and ExtWithdraw(X,a) over the same address/amount now produce
distinct digests. Off-chain signatures must match this exactly or on-chain
verification fails once #38 merges.

Add a known-answer cross-check test asserting the SDK digest equals the
contract's hash_payload output for fixed vectors (deposit, withdraw, and a
mixed deposit+create list), including deposit != withdraw.

Patch bump 0.6.1 -> 0.6.2.
@AquiGorka AquiGorka requested review from fazzatti and toruguera July 8, 2026 15:25
@AquiGorka AquiGorka closed this Jul 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant